package com.example.anliku.service.serviceImpl;

import com.example.anliku.entity.Message;
import com.example.anliku.entity.Team;
import com.example.anliku.entity.TeamMemberNew;
import com.example.anliku.entity.User;
import com.example.anliku.entity.teamBody.TeamMemberBody;
import com.example.anliku.entity.teamBody.TeamMemberReturnBody;
import com.example.anliku.mapper.TeamMapper;
import com.example.anliku.mapper.TeamMemberNewMapper;
import com.example.anliku.mapper.UserMapper;
import com.example.anliku.service.TeamMemberNewService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class TeamMemberNewServiceImpl implements TeamMemberNewService {

    @Resource
    private TeamMemberNewMapper teamMemberNewMapper;

    @Resource
    private TeamMapper teamMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Message deleteByPrimaryKey(Integer teamMemberNewId) {
        return teamMemberNewMapper.deleteByPrimaryKey(teamMemberNewId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(TeamMemberNew record) {
        return teamMemberNewMapper.insert(record);
    }

    @Override
    public Message insertSelective(TeamMemberNew record) {
        return teamMemberNewMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public TeamMemberNew selectByPrimaryKey(Integer teamMemberNewId) {
        return teamMemberNewMapper.selectByPrimaryKey(teamMemberNewId);
    }

    @Override
    public int updateByPrimaryKeySelective(TeamMemberNew record) {
        return teamMemberNewMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TeamMemberNew record) {
        return teamMemberNewMapper.updateByPrimaryKey(record);
    }


    @Override
    public Message insertList(List<TeamMemberNew> list) {
        return teamMemberNewMapper.insertList(list) > 0 ? Message.success(null) : Message.fail(null);


    }

    @Override
    public Message selectAllByTeamMemberNewUserId(String teamMemberNewUserId) {
        return Message.success(null).add(teamMemberNewMapper.selectAllByTeamMemberNewUserId(teamMemberNewUserId));
    }

    @Override
    public Message deleteByTeamMemberNewUserId(String teamMemberNewUserId) {
        return teamMemberNewMapper.deleteByTeamMemberNewUserId(teamMemberNewUserId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectTeamMemberNewIdByTeamMemberNewUserId(String teamMemberNewUserId) {
        List<Integer> integerList = teamMemberNewMapper.selectTeamNewIdByTeamMemberNewUserId(teamMemberNewUserId);
        List<Map> mapList = new LinkedList<>();
        for (Integer integer : integerList) {
            HashMap hashMap = new HashMap();
            Team team = teamMapper.selectByPrimaryKey(integer);
            hashMap.put("teamName", team.getTeamName());
            List<String> stringList = teamMemberNewMapper.selectTeamMemberNewUserIdByTeamNewId(integer);
            List<Map> userList = new LinkedList<>();
            for (String string : stringList) {
                HashMap hashMap1 = new HashMap();
                String name = userMapper.selectUserRealnameByUserIdAndUserState(string, 0);
                hashMap1.put("userId", string);
                hashMap1.put("userRealname", name);
                userList.add(hashMap1);
            }
            hashMap.put("teamNumber", userList);
            mapList.add(hashMap);
        }
        return Message.success(null).add(mapList);
    }


    @Override
    public int updateBatch(List<TeamMemberBody> list) {
        return teamMemberNewMapper.updateBatch(list);
    }

    @Override
    public Message batchInsert(List<TeamMemberReturnBody> list) {
        List<TeamMemberBody> teamMemberBodies=new ArrayList<>();
        for(TeamMemberReturnBody teamMemberReturnBody:list) {
            Team team=teamMapper.selectByPrimaryKey(teamMemberReturnBody.getTeamNewId());
            if(team == null){
                return Message.fail("团队id不存在");
            }
            User user=userMapper.selectByPrimaryKey(teamMemberReturnBody.getTeamMemberNewUserId());
            if(user == null){
                return Message.fail("用户ID不存在");
            }
            List<TeamMemberNew>  aaa = teamMemberNewMapper.selectByTeamNewIdAndTeamMemberNewUserId(teamMemberReturnBody.getTeamNewId(), teamMemberReturnBody.getTeamMemberNewUserId());
            if (CollectionUtils.isNotEmpty(aaa)) {
                return Message.fail("成员添加重复");
            }
            teamMemberBodies.add(TeamMemberBody.get(teamMemberReturnBody));
        }
        return teamMemberNewMapper.batchInsert(teamMemberBodies) > 0 ? Message.success(null) : Message.fail(null);
    }


}

